const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

module.exports = {
  mode: 'production',
  // 入口
  entry: "./src/index.ts",
  // 打包
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: "bundle.js",
    environment: {
      arrowFunction: false, // 关闭webpack的箭头函数，可选
      const: false
    }
  },
  // 设置引入模块的后缀名,使的在引入文件时可以不用写后缀
  resolve: {
    extensions: [".ts", ".js"]
  },
  //模块
  module: {
    // 知道加载器的规则
    rules: [
      {
        test: /\.ts$/,
        use: [
          {
            loader: "babel-loader",
            options:{
              presets: [
                [
                  "@babel/preset-env",
                  {
                    "targets":{
                      "chrome": "58",
                      "ie": "10"
                    },
                    "corejs":"3",
                    // usage按需加载
                    "useBuiltIns": "usage"
                  }
                ]
              ]
            }
          },
          "ts-loader"
        ],
        exclude: /node_modules/
      },
      {
        test: /\.less$/,
        use: [
          // compiles Less to CSS
          'style-loader',
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                // 插件
                plugins: [
                  [
                    'postcss-preset-env',
                    {
                      // 其他选项
                      browsers: "last 2 versions"
                    },
                  ],
                ],
              },
            },
          },
          'less-loader',
        ],
      },
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
        template: "./src/index.html"
    }),
  ]
}